<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PDF 文字识别</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .upload-section {
            margin: 20px 0;
            text-align: center;
        }
        .file-input {
            display: none;
        }
        .upload-button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .upload-button:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 20px;
            white-space: pre-wrap;
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 4px;
            border: 1px solid #dee2e6;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        .error {
            color: red;
            margin: 10px 0;
            display: none;
        }
        .confidence {
            color: #666;
            font-size: 0.9em;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>PDF 文字识别</h1>
        
        <div class="upload-section">
            <input type="file" id="fileInput" class="file-input" accept=".pdf">
            <button class="upload-button" onclick="document.getElementById('fileInput').click()">
                选择 PDF 文件
            </button>
            <div id="fileName"></div>
        </div>

        <div id="loading" class="loading">
            正在处理，请稍候...
        </div>

        <div id="error" class="error"></div>

        <div id="result" class="result" style="display: none;">
            <div id="textResult"></div>
            <div id="confidenceResult" class="confidence"></div>
        </div>
    </div>

    <script>
        document.getElementById('fileInput').addEventListener('change', function(e) {
            const fileName = e.target.files[0]?.name;
            if (fileName) {
                document.getElementById('fileName').textContent = `已选择: ${fileName}`;
                uploadFile(e.target.files[0]);
            }
        });

        async function uploadFile(file) {
            const loading = document.getElementById('loading');
            const error = document.getElementById('error');
            const result = document.getElementById('result');
            const textResult = document.getElementById('textResult');
            const confidenceResult = document.getElementById('confidenceResult');

            loading.style.display = 'block';
            error.style.display = 'none';
            result.style.display = 'none';

            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch('/ocr_pdf/', {
                    method: 'POST',
                    body: formData
                });

                if (response.ok) {
                    const data = await response.json();
                    
                    // 显示识别的文本
                    textResult.innerHTML = data.text.map((text, index) => {
                        const confidence = (data.confidence[index] * 100).toFixed(2);
                        return `<div>
                            <div>${text}</div>
                            <div class="confidence">置信度: ${confidence}%</div>
                        </div>`;
                    }).join('<hr>');
                    
                    result.style.display = 'block';
                } else {
                    const errorData = await response.json();
                    throw new Error(errorData.detail || '处理失败');
                }
            } catch (err) {
                error.textContent = '错误：' + err.message;
                error.style.display = 'block';
            } finally {
                loading.style.display = 'none';
            }
        }
    </script>
</body>
</html> 